﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyLibrary.API.Ext
{
	/// <summary>
	/// Definition of the Ext.object this action will create as a new tab.
	/// </summary>
	public enum ToolType
	{
		/// <summary>
		/// A new tab that contains a grid only.
		/// </summary>
		Grid = 1,
		/// <summary>
		/// A new tab that contains a graph only.
		/// </summary>
		Graph = 2,
		/// <summary>
		/// A new tab that will be added via an Ext object returned from the indicated JavaScript function.
		/// </summary>
		JSFunc = 3
	}

	/// <summary>
	/// Class defining a new click-action used by an Ext.tree.TreePanel object reading the associated array Node item.
	/// </summary>
	public class LoadTab
	{
		public string DomID { get; set; }
		public ToolType Tool { get; set; }
		public string FrameType { get; set; }
		public string Url { get; set; }
		public string Cmd { get; set; }
		public Dictionary<string,object> Params { get; set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="id"></param>
		/// <param name="cmd"></param>
		/// <param name="pars"></param>
		public LoadTab(string id, string cmd, Dictionary<string, object> pars)
		{
			this.DomID = id;
			this.Cmd = cmd;
			this.Params = pars ?? new Dictionary<string, object>();
			this.Tool = ToolType.JSFunc;
		}

		/// <summary>
		/// Creates a new Tab action that will create a new Grid on click.
		/// </summary>
		/// <param name="id">New Tab's Client-side ID</param>
		/// <param name="frametype">Frame Config Key to use - the Key from the application's Frame Config section inside the LoadFrame function.</param>
		/// <param name="dataUrl">Source URL to call in Grid's ajax data requests.</param>
		/// <param name="cmd">Unique cmd operation parameter to send to URL.</param>
		public LoadTab(string id, string frametype, string dataUrl, string cmd)
		{
			this.DomID = id;
			this.Tool = ToolType.Grid;
			this.FrameType = frametype;
			this.Url = dataUrl;
			this.Cmd = cmd;
		}

		/// <summary>
		/// Creates a new Tab action that will create the indicated Ext.object type on click.
		/// </summary>
		/// <param name="id">New Tab's Client-side ID</param>
		/// <param name="SetType">Object type indicator - which Ext object definition to use.</param>
		/// <param name="frametype">Frame Config Key to use - the Key from the application's Frame Config section inside the LoadFrame function.</param>
		/// <param name="dataUrl">Source URL to call in Grid's ajax data requests.</param>
		/// <param name="cmd">Unique cmd operation parameter to send to URL.</param>
		public LoadTab(string id, ToolType SetType, string frametype, string dataUrl, string cmd)
		{
			this.DomID = id;
			this.Tool = SetType;
			this.FrameType = frametype;
			this.Url = "";
			this.Cmd = "";
		}

		private string RenderParams()
		{
			string Params = "{";
			foreach (string Key in this.Params.Keys)
			{
				Params += "" + Key + ": \"" + this.Params[Key] + "\", ";
			}
			if (Params.Length > 1)
			{
				Params = Params.Substring(0, Params.Length - 2);
			}
			return Params + "}";
		}

		/// <summary>
		/// Renders object into Ext.config format.
		/// </summary>
		/// <returns></returns>
		public string Render()
		{
			if (this.Tool == ToolType.JSFunc)
			{
				return "{" + string.Format(" id: \"{0}\", toolType: {1}, cmd: \"{2}\", params: {3} ", DomID, Convert.ToInt32(Tool), Cmd, this.RenderParams()) + "}";
			}
			else
			{
				return "{" + string.Format(" id: \"{0}\", toolType: {1}, baseType: \"{2}\", dataUrl: \"{3}\", cmd: \"{4}\" ", DomID, Convert.ToInt32(Tool), FrameType, Url, Cmd) + "}";
			}
		}
	}
}
